package com.example.vueadmin.service;

import com.example.vueadmin.entity.*;
import com.example.vueadmin.mapper.InventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    public Map<String, Object> select(InventoryVO inventoryVO) {
        Integer pageNum = inventoryVO.getPageNum();
        Integer pageSize = inventoryVO.getPageSize();
        pageNum = (pageNum - 1) * pageSize;
        inventoryVO.setPageNum(pageNum);
        Integer total = inventoryMapper.total(inventoryVO);
        List<Inventory> data = inventoryMapper.selectPage(inventoryVO);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    public Integer enter(Enter enter) {
        enter.setEnterTime(new Date());
        inventoryMapper.enterLog(enter);
        Inventory inventory = new Inventory();
        inventory.setInnerTime(new Date());
        inventory.setGoodsName(inventoryMapper.goodsName(enter.getGoodsId()));
        inventory.setGoodsId(enter.getGoodsId());
        inventory.setWareHouseName(inventoryMapper.wareHouseName(enter.getWareHouseId()));
        inventory.setWareHouseId(enter.getWareHouseId());
        inventory.setInventory(enter.getCount());
        inventory.setQuality(enter.getQuality());
        return inventoryMapper.enter(inventory);
    }

    public Integer outer(Outer outer) {
        outer.setOuterTime(new Date());
        Inventory inventory = new Inventory();
        inventory.setInventoryCode(outer.getInventoryCode());
        inventory.setWareHouseId(outer.getWareHouseId());
        inventory.setGoodsId(outer.getGoodsId());
        inventory.setGoodsName(outer.getGoodsName());
        inventory.setWareHouseName(outer.getWareHouseName());
        Integer inventoryCount = inventoryMapper.selectCount(inventory);
        if (inventoryCount > outer.getCount()) {
            inventory.setInventory(inventoryCount-outer.getCount());
            inventoryMapper.outerLog(outer);
            inventoryMapper.update(inventory);
            return outer.getCount();
        } else if (inventoryMapper.selectCount(inventory) == outer.getCount()) {
            inventoryMapper.outerLog(outer);
            inventoryMapper.outer(inventory);
            return outer.getCount();
        } else {
            return 0;
        }


    }

//    public String enter(Enter enter) {
//        Goods goods = new Goods();
//        WareHouse wareHouse = new WareHouse();
//        Inventory inventory = new Inventory();
//        Date date = new Date();
//        enter.setEnterTime(date);
//        enter.setGoodsName(inventoryMapper.goodsName(enter.getGoodsId()));
//        enter.setWareHouseName(inventoryMapper.wareHouseName(enter.getWareHouseId()));
//        inventoryMapper.enterLog(enter);
//        if (inventoryMapper.selectGoods(enter).size() <= 0) {
//            goods.setGoodsId(enter.getGoodsId());
//            wareHouse.setWareHouseId(enter.getWareHouseId());
//            goods.setGoodsName(inventoryMapper.goodsName(goods.getGoodsId()));
//            wareHouse.setWareHouseName(inventoryMapper.wareHouseName(wareHouse.getWareHouseId()));
//            inventory.setGoodsId(goods.getGoodsId());
//            inventory.setGoodsName(goods.getGoodsName());
//            inventory.setWareHouseId(wareHouse.getWareHouseId());
//            inventory.setWareHouseName(wareHouse.getWareHouseName());
//            inventory.setInventory(enter.getCount());
//            inventory.setQuality(3);
//            inventoryMapper.enter(inventory);
//            return ("新增了" + inventory.getInventory() + "个货物");
//
//        } else {
//            Integer count = enter.getCount() + inventoryMapper.selectGoods(enter).get(0).getInventory();
//            inventoryMapper.updateInventory(count, inventoryMapper.selectGoods(enter).get(0).getInventoryCode());
//            return ("新增了" + enter.getCount() + "个货物");
//
//        }
//
//    }

//    public String outer(Outer outer) {
//        Date date = new Date();
//        outer.setOuterTime(date);
//        Enter enter = new Enter();
//        enter.setGoodsId(outer.getGoodsId());
//        enter.setWareHouseId(outer.getWareHouseId());
//        enter.setGoodsName(inventoryMapper.goodsName(enter.getGoodsId()));
//        enter.setWareHouseName(inventoryMapper.wareHouseName(enter.getWareHouseId()));
//        if (inventoryMapper.selectGoods(enter).size() <= 0) {
//            return "货物不存在";
//        } else {
//            Integer inventoryCode = inventoryMapper.selectGoods(enter).get(0).getInventoryCode();
//            Integer count = inventoryMapper.selectGoods(enter).get(0).getInventory();
//            if (outer.getCount() > count) {
//                return "库存不足";
//            } else {
//                if (outer.getType() == "销货出库") {
//                    outer.setSale(0);
//                }
//                inventoryMapper.outerLog(outer);
//                outer.setCount(count - outer.getCount());
//                inventoryMapper.updateInventory(outer.getCount(), inventoryCode);
//                return ("出库了" + (count - outer.getCount()) + "个货物");
//            }
//        }
//
//    }

    public List<Enter> queryEnter() {
        return inventoryMapper.queryEnter();
    }

    public List<Outer> queryOuter() {
        return inventoryMapper.queryOuter();
    }
}
